#!/bin/tcsh -ef
####
#
#############################################################################
#                                                                           #
# Title: Unbend II                                                          #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 02/20/2006                                             #
# Last Modification: 02/20/2006                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# MANUAL: This script will do the second and following unbending rounds. 
#
# MANUAL: The unbending always starts with the raw image ${imagename}.mrc, but uses the latest verion of the reference as the unbending template. The number of maskb?? values that are not "0" determine how many rounds of unbending will be done.
#
#
# PUBLICATION: 2dx - User-friendly image processing for 2D crystals: <A HREF="http://dx.doi.org/10.1016/j.jsb.2006.07.020">J. Struct. Biol. 157 (2007) 64-72</A>
# PUBLICATION: Image processing of 2D crystal images: <A HREF="http://link.springer.com/protocol/10.1007%2F978-1-62703-176-9_10">Methods in Molecular Biology (2012)</A>
#
# SORTORDER: 50
#
# DISPLAY: holeb
# DISPLAY: maskb01
# DISPLAY: maskb02
# DISPLAY: maskb03
# DISPLAY: maskb04
# DISPLAY: maskb05
# DISPLAY: boxb1
# DISPLAY: boxb2
# DISPLAY: quadradb
# DISPLAY: facthreshb
# DISPLAY: quadpredb
# DISPLAY: RESMAX
# DISPLAY: RESMIN
# DISPLAY: ALAT
# DISPLAY: radlim
# DISPLAY: domask
# DISPLAY: treatspotscan
# DISPLAY: ctfplotresmax
# DISPLAY: tempkeep
# DISPLAY: createmaskinfo
# DISPLAY: ISTEP
# DISPLAY: ISTEP_h
# DISPLAY: IMAXCOR
# DISPLAY: comment
# DISPLAY: RMAG
# DISPLAY: LCOLOR
# DISPLAY: ccunbend_program
#
#$end_local_vars
#
#
set bin_2dx = ""
set proc_2dx = ""
set app_2dx_mrc_converter = ""
#
set PHASEORI_done = ""
set boxb1 = ""
set boxb2 = ""
set quadradb = ""
set imagename = ""
set defocus = ""
set nonmaskimagename = ""
set imagenumber = ""
set realcell = ""
set lattice = ""
set holeb = ""
set maskb01 = ""
set maskb02 = ""
set maskb03 = ""
set maskb04 = ""
set maskb05 = ""
set imagesidelength = ""
set magnification = ""
set stepdigitizer = ""
set tempkeep = ""
set RESMIN = ""
set RESMAX = ""
set ALAT = ""
set quadpredb = ""
set radlim = ""
set realang = ""
set treatspotscan = ""
set facthresha = ""
set phacon = ""
set ctfplotresmax = ""
set stepdigitizer = ""
set CS = ""
set KV = ""
set domask = ""
set TLTAXIS = ""
set TLTANG = ""
set TANGL = ""
set TLTAXA = ""
set TAXA = ""
set det_tilt = ""
set createmaskinfo = ""
set taperBeforeFFT = ""
set generatePeriodogram = ""
set periodogramWindowsize = ""
set periodogramNumsubsteps = ""
set ISTEP = ""
set ISTEP_h = ""
set IMAXCOR = ""
set RMAG = ""
set LCOLOR = ""
set RB_1 = ""
set RB_2 = ""
set RB_3 = ""
set RB_4 = ""
set RB_5 = ""
set RB_6 = ""
set RP_1 = ""
set RP_2 = ""
set RP_3 = ""
set RP_4 = ""
set RP_5 = ""
set RP_6 = ""
set crop = ""
set crop_histogram = ""
set crop_histogram_percent = ""
set crop_histogram_stdev = ""
set use_masked_image = ""
set ctfcor_imode = ""
set ctfcor_noise = ""
set ctfcor_debug = ""
set Thread_Number = ""
set movie_inmovie = ""
set ccunbend_program = ""
#
#$end_vars
#

set scriptname = 2dx_unbend2
\rm -f LOGS/${scriptname}.results
#
echo "<<@evaluate>>"
#
set IS_2DX = yes
source ${proc_2dx}/initialize
#
set date = `date`
echo date = ${date}
#
if ( ${ctfcor_imode}x == 9x ) then
  set iname = image_ctfcor_multiplied
else
  set iname = image_ctfcor
endif
#
if ( x${TLTAXIS} == "x-" || x${TLTAXIS} == "x--" ) then
  set TLTAXIS = "0.0"
endif
if ( x${TLTANG} == "x-" || x${TLTANG} == "x--" ) then
  set TLTANG = "0.0"
endif
if ( x${TLTAXA} == "x-" || x${TLTAXA} == "x--" ) then
  set TLTAXA = "0.0"
endif
if ( x${TAXA} == "x-" || x${TAXA} == "x--" ) then
  set TAXA = "0.0"
endif
if ( x${TANGL} == "x-" || x${TANGL} == "x--" ) then
  set TANGL = "0.0"
endif
#
#################################################################################
${proc_2dx}/linblock "Verifying some parameters"
#################################################################################
#
set tmp1 = `echo ${boxb1} | awk '{s = int( $1 ) } END { print s }'`
if ( ${tmp1} == ${boxb1} ) then
  echo boxb1 = ${boxb1}
else
  set boxb1 = ${tmp1}
  echo boxb1 = ${boxb1}
  echo "set boxb1 = ${boxb1}" >> LOGS/${scriptname}.results
  ${proc_2dx}/linblock "Warning: boxb1 needs to be an integer number. Now corrected."
  echo "#WARNING: Warning: boxb1 needs to be an integer number. Now corrected." >> LOGS/${scriptname}.results
endif
#
if ( ${boxb1} == "0" ) then
  set boxb1 = `echo ${imagesidelength} | awk '{ s = int( $1 / 13 ) } END { print s }'`
  echo "set boxb1 = ${boxb1}"  >> LOGS/${scriptname}.results
  echo ":Initializing boxb1 as ${boxb1}"
endif
#
set tmp1 = `echo ${boxb2} | awk '{s = int( $1 ) } END { print s }'`
if ( ${tmp1} == ${boxb2} ) then
  echo boxb2 = ${boxb2}
else
  set boxb2 = ${tmp1}
  echo boxb2 = ${boxb2}
  echo "set boxb2 = ${boxb2}" >> LOGS/${scriptname}.results
  ${proc_2dx}/linblock "Warning: boxb2 needs to be an integer number. Now corrected."
  echo "#WARNING: Warning: boxb2 needs to be an integer number. Now corrected." >> LOGS/${scriptname}.results
endif
#
set tmp1 = `echo ${boxb1} ${boxb2} | awk '{if ( $1 > $2 ) { s = 1 } else { s = 0 } } END { print s }'`
if ( ${tmp1} == 1 ) then
  set tmp1 = ${boxb2}
  set boxb2 = ${boxb1}
  set boxb1 = ${tmp1}
  echo boxb1 = ${boxb1}
  echo boxb2 = ${boxb2}
  echo "set boxb1 = ${boxb1}" >> LOGS/${scriptname}.results
  echo "set boxb2 = ${boxb2}" >> LOGS/${scriptname}.results
  ${proc_2dx}/linblock "Warning: boxb2 should be larger than boxb1. Now exchanged."
  echo "#WARNING: Warning: boxb2 should be larger than boxb1. Now exchanged." >> LOGS/${scriptname}.results
endif
#
set tmp1 = `echo ${maskb01} | awk '{s = int( $1 ) } END { print s }'`
if ( ${tmp1} == ${maskb01} ) then
  echo maskb01 = ${maskb01}
else
  set maskb01 = ${tmp1}
  echo maskb01 = ${maskb01}
  echo "set maskb01 = ${maskb01}" >> LOGS/${scriptname}.results
  ${proc_2dx}/linblock "Warning: maskb01 needs to be an integer number. Now corrected."
  echo "#WARNING: Warning: maskb01 needs to be an integer number. Now corrected." >> LOGS/${scriptname}.results
endif
#
set tmp1 = `echo ${maskb02} | awk '{s = int( $1 ) } END { print s }'`
if ( ${tmp1} == ${maskb02} ) then
  echo maskb02 = ${maskb02}
else
  set maskb02 = ${tmp1}
  echo maskb02 = ${maskb02}
  echo "set maskb02 = ${maskb02}" >> LOGS/${scriptname}.results
  ${proc_2dx}/linblock "Warning: maskb02 needs to be an integer number. Now corrected."
  echo "#WARNING: Warning: maskb02 needs to be an integer number. Now corrected." >> LOGS/${scriptname}.results
endif
#
set tmp1 = `echo ${maskb03} | awk '{s = int( $1 ) } END { print s }'`
if ( ${tmp1} == ${maskb03} ) then
  echo maskb03 = ${maskb03}
else
  set maskb03 = ${tmp1}
  echo maskb03 = ${maskb03}
  echo "set maskb03 = ${maskb03}" >> LOGS/${scriptname}.results
  ${proc_2dx}/linblock "Warning: maskb03 needs to be an integer number. Now corrected."
  echo "#WARNING: Warning: maskb03 needs to be an integer number. Now corrected." >> LOGS/${scriptname}.results
endif
#
set tmp1 = `echo ${maskb04} | awk '{s = int( $1 ) } END { print s }'`
if ( ${tmp1} == ${maskb04} ) then
  echo maskb04 = ${maskb04}
else
  set maskb04 = ${tmp1}
  echo maskb04 = ${maskb04}
  echo "set maskb04 = ${maskb04}" >> LOGS/${scriptname}.results
  ${proc_2dx}/linblock "Warning: maskb04 needs to be an integer number. Now corrected."
  echo "#WARNING: Warning: maskb04 needs to be an integer number. Now corrected." >> LOGS/${scriptname}.results
endif
#
set tmp1 = `echo ${maskb05} | awk '{s = int( $1 ) } END { print s }'`
if ( ${tmp1} == ${maskb05} ) then
  echo maskb05 = ${maskb05}
else
  set maskb05 = ${tmp1}
  echo maskb05 = ${maskb05}
  echo "set maskb05 = ${maskb05}" >> LOGS/${scriptname}.results
  ${proc_2dx}/linblock "Warning: maskb05 needs to be an integer number. Now corrected."
  echo "#WARNING: Warning: maskb05 needs to be an integer number. Now corrected." >> LOGS/${scriptname}.results
endif
#
\rm -f HOLEref.TMP HOLE.TMP
echo "<<@progress: 1>>"
#
set round = 1
if ( ${maskb02} > '0' ) then
  @ round += 1
endif
if ( ${maskb03} > '0' ) then
  @ round += 1
endif
if ( ${maskb04} > '0' ) then
  @ round += 1
endif
if ( ${maskb05} > '0' ) then
  @ round += 1
endif
#
set startname = `echo ${imagename} | cut -c1`
#
if ( $domask == 'y' ) then
  if ( -e ${imagename}_mask.mrc ) then
    set domask = 'n'
    echo "set domask = ${domask}" >> LOGS/${scriptname}.results
    ${proc_2dx}/linblock "Masking info exists. domask corrected to ${domask}."
  endif
endif
#
if ( ${createmaskinfo} == 'y' ) then
  if ( ${startname} == 'm' ) then
    # set createmaskinfo = 'n'
    ${proc_2dx}/linblock "WARNING: creating another MASKINFO, but file already masked."
  endif
endif
#
set quadradbx = `echo ${quadradb} | sed 's/,/ /g' | awk '{ s = int( $1 ) } END { print s }'`
set quadradby = `echo ${quadradb} | sed 's/,/ /g' | awk '{ s = int( $2 ) } END { print s }'`
set tmp = `echo ${quadradbx} ${quadradby} | awk '{if ( $2 == 0 ) { s = $1 } else { s = $2 }} END { print s }'`
if ( ${tmp} != ${quadradby} ) then
  set quadradby = ${tmp}
  set quadradb = `echo ${quadradbx} ${quadradby} | sed 's/ /,/g'`
  echo "set quadradb = ${quadradb}" >> LOGS/${scriptname}.results
  ${proc_2dx}/linblock "Warning: correcting quadradb to ${quadradb}."
  echo "#WARNING: Warning: correcting quadradb to ${quadradb}." >> LOGS/${scriptname}.results
endif
#
set imagecenterx = `echo ${imagesidelength} | awk '{ s = int( $1 / 2 ) } END { print s }'`
set imagecentery = ${imagecenterx}
#
#
#################################################################################
#
if ( ${boxb1} > ${imagesidelength} ) then
  ${proc_2dx}/linblock "WARNING: boxb1 too large."
endif
#  
if ( ${boxb2} > ${imagesidelength} ) then
  ${proc_2dx}/linblock "WARNING: boxb2 too large."
endif
#  
if ( ${treatspotscan} == 'y' ) then
  set valspotscan = '1'
else
  set valspotscan = '0'
endif
#
set rmax = 11000
echo rmax = ${rmax}
#
# Limit RADLIM parameters to at least 20
set radlimx = `echo ${radlim} | cut -d\, -f1`
set radlimy = `echo ${radlim} | cut -d\, -f2`
set radlima = `echo ${radlim} | cut -d\, -f3`
set radlimtmpx = `echo ${radlimx} | awk '{if ( $1 < 20 ) { s = 20 } else { s = $1 }} END { print s }'`
set radlimtmpy = `echo ${radlimy} | awk '{if ( $1 < 20 ) { s = 20 } else { s = $1 }} END { print s }'`
set radlimnewx = `echo ${radlimtmpx} | awk '{if ( $1 > 49 ) { s = 49 } else { s = $1 }} END { print s }'`
set radlimnewy = `echo ${radlimtmpy} | awk '{if ( $1 > 49 ) { s = 49 } else { s = $1 }} END { print s }'`
set radlimnew = `echo ${radlimnewx},${radlimnewy},${radlima}`
if ( ${radlim} != ${radlimnew} ) then
  set radlim = ${radlimnew}
  echo "set radlim = ${radlim}" >> LOGS/${scriptname}.results
  ${proc_2dx}/linblock "WARNING: correcting RADLIM to ${radlim}."
endif
#
set reciangle = `echo ${realang} | awk '{ s = 180.0 - $1 } END { print s } '`
echo reciangle = ${reciangle}
#
set rtempx1 = ${imagecenterx}
set rtempy1 = ${imagecentery}
set rtempx2 = ${imagecenterx}
set rtempy2 = ${imagecentery}
@ rtempx1 -= 13
@ rtempx2 += 12
@ rtempy1 -= 13
@ rtempy2 += 12
# this gives a box at the reference location with a diameter of 26 pixels.
set patlabel = ${rtempx1},${rtempx2},${rtempy1},${rtempy2}
echo patlabel = ${patlabel}
#
set rtempx1 = ${imagecenterx}
set rtempy1 = ${imagecentery}
set rtempx2 = ${imagecenterx}
set rtempy2 = ${imagecentery}
@ rtempx1 -= 400
@ rtempx2 += 399
@ rtempy1 -= 400
@ rtempy2 += 399
# this gives a box at the reference locations with a diameter of 800 pixels.
set boxlabel = ${rtempx1},${rtempx2},${rtempy1},${rtempy2}
echo boxlabel = ${boxlabel}
#
# set RESPLOTMAX = 0.3
set RESPLOTMAX = `echo ${ctfplotresmax} | awk '{ if ( $1 > 0.1 ) { s = 1.0 / $1 } else { s = 0.3 } } END { print s }'`
# 0.3 corresponds to 3.33 Angstroem for the border of the plot.
#
if ( ! -d SCRATCH ) then
  ${proc_2dx}/protest "ERROR: SCRATCH does not exist."
endif
#
set u1 = `echo ${lattice} | cut -d\, -f1`
set u2 = `echo ${lattice} | cut -d\, -f2`
set v1 = `echo ${lattice} | cut -d\, -f3`
set v2 = `echo ${lattice} | cut -d\, -f4`
set ulen = `echo ${u1} ${u2} | awk '{ s = sqrt ( $1 * $1 + $2 * $2 ) } END { print s }'`
set vlen = `echo ${v1} ${v2} | awk '{ s = sqrt ( $1 * $1 + $2 * $2 ) } END { print s }'`
#
set latlenok = `echo ${ulen} ${vlen} | awk '{ if ( $1 + $2 < 0.1 ) { s = 0 } else { s = 1 }} END { print s }'`
if ( ${latlenok} == '0' ) then
  ${proc_2dx}/linblock "ERROR: Lattice is ${lattice}"
  ${proc_2dx}/protest "Determine lattice first. Aborting."
endif
#
if ( ! -e ${nonmaskimagename}.spt ) then
  echo ":: "
  ${proc_2dx}/linblock "Spotlist ${nonmaskimagename}.spt missing."
  ${proc_2dx}/protest "Aborting."
endif
#
#############################################################################
#                                                                           #
#  UNBEND II expects the existence of FFTIR/${imagename}_unbend1_fft.mrc #
#                                                                           #
#############################################################################
#
if ( ! -e FFTIR/${iname}_unbend1_fft.mrc ) then
  ${proc_2dx}/protest "ERROR: FFTIR/${iname}_unbend1_fft.mrc not existing. First run Unbend I."
endif
#
\cp -f FFTIR/${iname}_unbend1_fft.mrc FFTIR/${iname}_fou_unbend2_fft.mrc
#
echo "<<@progress: 5>>"
#
set u1 = `echo ${lattice} | cut -d\, -f1`
set u2 = `echo ${lattice} | cut -d\, -f2`
set v1 = `echo ${lattice} | cut -d\, -f3`
set v2 = `echo ${lattice} | cut -d\, -f4`
set ulen = `echo ${u1} ${u2} | awk '{ s = int( sqrt ( $1 * $1 + $2 * $2 ) ) } END { print s }'`
set vlen = `echo ${v1} ${v2} | awk '{ s = int( sqrt ( $1 * $1 + $2 * $2 ) ) } END { print s }'`
set smaller_vector = `echo ${ulen} ${vlen} | awk '{ if ( $1 < $2 ) { s = $1 } else { s = $2 }} END { print s }'`
#
set newmaskb01 = `echo ${smaller_vector} ${maskb01} | awk '{ if ( $1 / 2.1 < $2 ) { s = int( $1 / 2.1 ) } else { s = $2 }} END { print s }'`
set newmaskb01 = `echo ${newmaskb01} | awk '{ if ( $1 < 2 ) { s = 2 } else { s = $1 }} END { print s }'`
if ( ${maskb01} != ${newmaskb01} ) then
  ${proc_2dx}/linblock "correcting maskb01 from ${maskb01} to ${newmaskb01}"
  set maskb01 = ${newmaskb01}
  echo "set maskb01 = ${newmaskb01}" >> LOGS/${scriptname}.results
endif
${proc_2dx}/linblock "Using maskb01=${maskb01} for lattice of length u=${ulen} and v=${vlen}"
#
set newmaskb02 = `echo ${smaller_vector} ${maskb02} | awk '{ if ( $1 / 2.1 < $2 ) { s = int( $1 / 2.1 ) } else { s = $2 }} END { print s }'`
if ( ${maskb02} != ${newmaskb02} ) then
  ${proc_2dx}/linblock "correcting maskb02 from ${maskb02} to ${newmaskb02}"
  set maskb02 = ${newmaskb02}
  echo "set maskb02 = ${newmaskb02}" >> LOGS/${scriptname}.results
endif
if ( ${maskb02} != 0 ) then
  ${proc_2dx}/linblock "Using maskb02=${maskb02} for lattice of length u=${ulen} and v=${vlen}"
endif
#
set newmaskb03 = `echo ${smaller_vector} ${maskb03} | awk '{ if ( $1 / 2.1 < $2 ) { s = int( $1 / 2.1 ) } else { s = $2 }} END { print s }'`
if ( ${maskb03} != ${newmaskb03} ) then
  ${proc_2dx}/linblock "correcting maskb03 from ${maskb03} to ${newmaskb03}"
  set maskb03 = ${newmaskb03}
  echo "set maskb03 = ${newmaskb03}" >> LOGS/${scriptname}.results
endif
if ( ${maskb03} != 0 ) then
  ${proc_2dx}/linblock "Using maskb03=${maskb03} for lattice of length u=${ulen} and v=${vlen}"
endif
#
set newmaskb04 = `echo ${smaller_vector} ${maskb04} | awk '{ if ( $1 / 2.1 < $2 ) { s = int( $1 / 2.1 ) } else { s = $2 }} END { print s }'`
if ( ${maskb04} != ${newmaskb04} ) then
  ${proc_2dx}/linblock "correcting maskb04 from ${maskb04} to ${newmaskb04}"
  set maskb04 = ${newmaskb04}
  echo "set maskb04 = ${newmaskb04}" >> LOGS/${scriptname}.results
endif
if ( ${maskb04} != 0 ) then
  ${proc_2dx}/linblock "Using maskb04=${maskb04} for lattice of length u=${ulen} and v=${vlen}"
endif
#
set newmaskb05 = `echo ${smaller_vector} ${maskb05} | awk '{ if ( $1 / 2.1 < $2 ) { s = int( $1 / 2.1 ) } else { s = $2 }} END { print s }'`
if ( ${maskb05} != ${newmaskb05} ) then
  ${proc_2dx}/linblock "correcting maskb05 from ${maskb05} to ${newmaskb05}"
  set maskb05 = ${newmaskb05}
  echo "set maskb05 = ${newmaskb05}" >> LOGS/${scriptname}.results
endif
if ( ${maskb05} != 0 ) then
  ${proc_2dx}/linblock "Using maskb05=${maskb05} for lattice of length u=${ulen} and v=${vlen}"
endif
#
#############################################################################
#############################################################################
##### BEGINNING OF THE BIG WHILE LOOP OVER THE DIFFERENT MASKB VALUES #######
#############################################################################
#############################################################################
#
set locround = 1
set final_round = "n"
#
while ( ${locround} <= ${round} ) 
  #
  if ( ${locround} == ${round} ) then
    set final_round = "y" 
  endif
  #
  source ${proc_2dx}/2dx_unbend2_sub.com
  #
  echo "<<@evaluate>>"
  #
  @ locround += 1
  #
end
#
#
#
#
#############################################################################
${proc_2dx}/${lincommand} "MASKTRAN - to mask the FFT of the unbent image"
#############################################################################
#
echo "# IMAGE-IMPORTANT: unbent.mrc <Unbent image>" >> LOGS/${scriptname}.results
\cp -f FFTIR/${iname}_fou_unbend2_fft.mrc FFTIR/${iname}_fou_unbent_fft.mrc
echo "# IMAGE-IMPORTANT: FFTIR/${iname}_fou_unbent_fft.mrc <Unbent image (FFT)>" >> LOGS/${scriptname}.results
#
\rm -f SCRATCH/${iname}_fou_unbent_fft_msk.mrc
#
setenv IN  FFTIR/${iname}_fou_unbent_fft.mrc
setenv OUT SCRATCH/${iname}_fou_unbent_fft_msk.mrc
# ITYPE=0 uses all spots (below). The following line is thereby probably not needed:
setenv SPOTS ${nonmaskimagename}.spt
#
${bin_2dx}/2dx_masktrana.exe << eot
1 T T F ! ISHAPE=1(CIRC),2(GAUSCIR),3(RECT)HOLE,IAMPLIMIT(T or F),ISPOT,IFIL
1  ! RADIUS OF HOLE IF CIRCULAR, X,Y HALF-EDGE-LENGTHS IF RECT.
${lattice} -100 100 -100 100 ${rmax} 0 !A/BX/Y,IH/IKMN/MX,RMAX,ITYPE
eot
#
#############################################################################
${proc_2dx}/linblock "2dx_fftrans - to create a Fourier-filtered direct reconstruction"
#############################################################################
\rm -f SCRATCH/${iname}_fou_unbent_fft_msk_fft.mrc
setenv IN SCRATCH/${iname}_fou_unbent_fft_msk.mrc
setenv OUT SCRATCH/${iname}_fou_unbent_fft_msk_fft.mrc
${bin_2dx}/2dx_fftrans.exe 
#
echo "# IMAGE: SCRATCH/${iname}_fou_unbent_fft_msk_fft.mrc <Unbent image, Fourier filtered (1px)>" >> LOGS/${scriptname}.results
if ( ${tempkeep} != 'y' ) then
  \rm -f SCRATCH/${iname}_fou_unbent_fft_msk.mrc
endif
#
  #############################################################################
  ${proc_2dx}/linblock "Measuring signal in resolution bins"
  #############################################################################
  #
  \rm -f SCRATCH/TMP9882.dat
  \rm -f SCRATCH/dummy.aph
  set rlocmin = ${RESMIN}
  set rlocmax = ${RB_1}
  #
  ${bin_2dx}/2dx_mmboxa.exe << eot
FFTIR/${iname}_fou_unbend2_fft.mrc
${imagenumber} ${iname}, Unbend2, ${date}
Y                               ! Use grid units?
Y                               ! Generate grid from lattice?
N                               ! Generate points from lattice?
2,2,0,100,100,19,19               ! IPIXEL,IOUT,NUMSPOT,NOH,NOK,NHOR,NVERT
SCRATCH/dummy.aph
SCRATCH/TMP9882.dat
U2
${imagecenterx},${imagecentery}         ! XORIG,YORIG
${rlocmin},${rlocmax},1,${realcell},${ALAT},${realang} ! RINNER,ROUTER,IRAD,A,B,W,ABANG
${lattice}                              ! Lattice vectors
eot
  #
  cat SCRATCH/TMP9882.dat | grep PSMAX > SCRATCH/TMP9881.dat
  source SCRATCH/TMP9881.dat
  set RP_1 = ${PSMAX}
  #
  \rm -f SCRATCH/TMP9882.dat
  \rm -f SCRATCH/dummy.aph
  set rlocmin = ${rlocmax}
  set rlocmax = ${RB_2}
  #
  ${bin_2dx}/2dx_mmboxa.exe << eot
FFTIR/${iname}_fou_unbend2_fft.mrc
${imagenumber} ${iname}, Unbend2, ${date}
Y                               ! Use grid units?
Y                               ! Generate grid from lattice?
N                               ! Generate points from lattice?
2,2,0,100,100,19,19               ! IPIXEL,IOUT,NUMSPOT,NOH,NOK,NHOR,NVERT
SCRATCH/dummy.aph
SCRATCH/TMP9882.dat
U2
${imagecenterx},${imagecentery}         ! XORIG,YORIG
${rlocmin},${rlocmax},1,${realcell},${ALAT},${realang} ! RINNER,ROUTER,IRAD,A,B,W,ABANG
${lattice}                              ! Lattice vectors
eot
  #
  cat SCRATCH/TMP9882.dat | grep PSMAX > SCRATCH/TMP9881.dat
  source SCRATCH/TMP9881.dat
  set RP_2 = ${PSMAX}
  #
  \rm -f SCRATCH/TMP9882.dat
  \rm -f SCRATCH/dummy.aph
  set rlocmin = ${rlocmax}
  set rlocmax = ${RB_3}
  #
  ${bin_2dx}/2dx_mmboxa.exe << eot
FFTIR/${iname}_fou_unbend2_fft.mrc
${imagenumber} ${iname}, Unbend2, ${date}
Y                               ! Use grid units?
Y                               ! Generate grid from lattice?
N                               ! Generate points from lattice?
2,2,0,100,100,19,19               ! IPIXEL,IOUT,NUMSPOT,NOH,NOK,NHOR,NVERT
SCRATCH/dummy.aph
SCRATCH/TMP9882.dat
U2
${imagecenterx},${imagecentery}         ! XORIG,YORIG
${rlocmin},${rlocmax},1,${realcell},${ALAT},${realang} ! RINNER,ROUTER,IRAD,A,B,W,ABANG
${lattice}                              ! Lattice vectors
eot
  #
  cat SCRATCH/TMP9882.dat | grep PSMAX > SCRATCH/TMP9881.dat
  source SCRATCH/TMP9881.dat
  set RP_3 = ${PSMAX}
  #
  \rm -f SCRATCH/TMP9882.dat
  \rm -f SCRATCH/dummy.aph
  set rlocmin = ${rlocmax}
  set rlocmax = ${RB_4}
  #
  ${bin_2dx}/2dx_mmboxa.exe << eot
FFTIR/${iname}_fou_unbend2_fft.mrc
${imagenumber} ${iname}, Unbend2, ${date}
Y                               ! Use grid units?
Y                               ! Generate grid from lattice?
N                               ! Generate points from lattice?
2,2,0,100,100,19,19               ! IPIXEL,IOUT,NUMSPOT,NOH,NOK,NHOR,NVERT
SCRATCH/dummy.aph
SCRATCH/TMP9882.dat
U2
${imagecenterx},${imagecentery}         ! XORIG,YORIG
${rlocmin},${rlocmax},1,${realcell},${ALAT},${realang} ! RINNER,ROUTER,IRAD,A,B,W,ABANG
${lattice}                              ! Lattice vectors
eot
  #
  cat SCRATCH/TMP9882.dat | grep PSMAX > SCRATCH/TMP9881.dat
  source SCRATCH/TMP9881.dat
  set RP_4 = ${PSMAX}
  #
  \rm -f SCRATCH/TMP9882.dat
  \rm -f SCRATCH/dummy.aph
  set rlocmin = ${rlocmax}
  set rlocmax = ${RB_5}
  #
  ${bin_2dx}/2dx_mmboxa.exe << eot
FFTIR/${iname}_fou_unbend2_fft.mrc
${imagenumber} ${iname}, Unbend2, ${date}
Y                               ! Use grid units?
Y                               ! Generate grid from lattice?
N                               ! Generate points from lattice?
2,2,0,100,100,19,19               ! IPIXEL,IOUT,NUMSPOT,NOH,NOK,NHOR,NVERT
SCRATCH/dummy.aph
SCRATCH/TMP9882.dat
U2
${imagecenterx},${imagecentery}         ! XORIG,YORIG
${rlocmin},${rlocmax},1,${realcell},${ALAT},${realang} ! RINNER,ROUTER,IRAD,A,B,W,ABANG
${lattice}                              ! Lattice vectors
eot
  #
  cat SCRATCH/TMP9882.dat | grep PSMAX > SCRATCH/TMP9881.dat
  source SCRATCH/TMP9881.dat
  set RP_5 = ${PSMAX}
  #
  \rm -f SCRATCH/TMP9882.dat
  \rm -f SCRATCH/dummy.aph
  set rlocmin = ${rlocmax}
  set rlocmax = ${RB_6}
  #
  ${bin_2dx}/2dx_mmboxa.exe << eot
FFTIR/${iname}_fou_unbend2_fft.mrc
${imagenumber} ${iname}, Unbend2, ${date}
Y                               ! Use grid units?
Y                               ! Generate grid from lattice?
N                               ! Generate points from lattice?
2,2,0,100,100,19,19               ! IPIXEL,IOUT,NUMSPOT,NOH,NOK,NHOR,NVERT
SCRATCH/dummy.aph
SCRATCH/TMP9882.dat
U2
${imagecenterx},${imagecentery}         ! XORIG,YORIG
${rlocmin},${rlocmax},1,${realcell},${ALAT},${realang} ! RINNER,ROUTER,IRAD,A,B,W,ABANG
${lattice}                              ! Lattice vectors
eot
  #
  cat SCRATCH/TMP9882.dat | grep PSMAX > SCRATCH/TMP9881.dat
  source SCRATCH/TMP9881.dat
  set RP_6 = ${PSMAX}
  #
  echo "::  ResBin     Power "
  echo ":: ${RB_1}      ${RP_1}"
  echo ":: ${RB_2}      ${RP_2}"
  echo ":: ${RB_3}      ${RP_3}"
  echo ":: ${RB_4}      ${RP_4}"
  echo ":: ${RB_5}      ${RP_5}"
  echo ":: ${RB_6}      ${RP_6}"
  #
  echo "set RP_1 = ${RP_1}" >> LOGS/${scriptname}.results
  echo "set RP_2 = ${RP_2}" >> LOGS/${scriptname}.results
  echo "set RP_3 = ${RP_3}" >> LOGS/${scriptname}.results
  echo "set RP_4 = ${RP_4}" >> LOGS/${scriptname}.results
  echo "set RP_5 = ${RP_5}" >> LOGS/${scriptname}.results
  echo "set RP_6 = ${RP_6}" >> LOGS/${scriptname}.results
  #
echo "set UNBENDING_done = y" >> LOGS/${scriptname}.results
echo "set ML_done = n" >> LOGS/${scriptname}.results
#
##########################################################################
${proc_2dx}/linblock "Prepare files for Library display."
##########################################################################
#
if ( -e ${nonmaskimagename}_automask.mrc ) then
  cp -f ${nonmaskimagename}_automask.mrc MaskingInfo.mrc
  \rm -f MaskingInfo.mrc.png
  ${app_2dx_mrc_converter} --size 400 MaskingInfo.mrc MaskingInfo.mrc.png
endif
#
if ( -e ManualMasking_UnbendPlot.mrc ) then
  \cp -f ManualMasking_UnbendPlot.mrc UnbendPlot.mrc
  \rm -f UnbendPlot.mrc.png
  ${app_2dx_mrc_converter} --size 400 UnbendPlot.mrc UnbendPlot.mrc.png
endif
#
\rm -f Image.mrc
\ln -s ${imagename}.mrc Image.mrc
\rm -f Image.mrc.png
${app_2dx_mrc_converter} --size 400 Image.mrc Image.mrc.png
#
echo ":: Done."
echo "<<@progress: 100>>"
#
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
#
exit
#
#
# These are listed here so that they show up in the GUI:
source ${proc_2dx}/2dx_fftrans_sub.com
python ${proc_2dx}/movie/apply_filter_fourier.py
python ${proc_2dx}/movie/mask.py
#
#

